package com.demo.service.impl;

import com.demo.Vo.LogsVO;
import com.demo.config.ThreadPoolTaskExecutorConfig;
import com.demo.config.redis.RedisUtil;
import com.demo.mapper.DemoMapper;
import com.demo.service.TestService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executor;

@Slf4j
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {

	@Resource
	private DemoMapper mapper;

	@Autowired
	private RedisUtil redisLock;

	@Resource
	private RedissonClient redissonClient;

	private RLock rLock;

	@Autowired
	private AmqpTemplate amqpTemplate;

	@Override
	public String insert() {
		ThreadPoolTaskExecutorConfig threadPoolTaskExecutorConfig = new ThreadPoolTaskExecutorConfig();

		// 创建线程池
		Executor executor = threadPoolTaskExecutorConfig.getAsyncExecutor();
		String rel = null;
		// 线程方法
		executor.execute(() -> {
			try {
				String str1 = new SimpleDateFormat("mmssSSS").format(new Date());
				String str2 = UUID.randomUUID().toString();
				String yyyyMMddhhmmss = new StringBuffer().append(str1).toString();

				String sout = sout(yyyyMMddhhmmss);
			}
			catch (Exception e) {
				throw new RuntimeException(e);
			}
		});
		return rel;
	}

	/**
	 * 加redis分布式锁
	 * @throws Exception
	 */
	private String sout(String yyyyMMddhhmmss) {
		// selectList();
		// 当前线程name
		String name = Thread.currentThread().getName();
		long id = Thread.currentThread().getId();

		// 手动lua脚本加锁操作
		try {
			if (redisLock.lock(yyyyMMddhhmmss, yyyyMMddhhmmss, 10)) {
				// 测试数据库插入数据操作
				int insert = mapper.insert(Integer.valueOf(yyyyMMddhhmmss));
				// 参数1：交换机名称，2：绑定队列与交换机的名称，3：传递的参数
				amqpTemplate.convertAndSend("topicExchange", "topic.test1.goods", yyyyMMddhhmmss);
				System.out.println(yyyyMMddhhmmss + "枷锁chenggong");
				return "枷锁成功";
			}
			else {
				System.out.println(yyyyMMddhhmmss + "枷锁失败");
				return "枷锁失败";
			}
		}
		catch (Exception e) {
			throw new RuntimeException(e);
			// throw e;
		}
		finally {
			redisLock.unlock(yyyyMMddhhmmss, yyyyMMddhhmmss);
		}

		// redisson加锁
		// RLock lock = redissonClient.getLock(yyyyMMddhhmmss);
		// try {
		// boolean b = lock.tryLock(1,15,TimeUnit.SECONDS);
		//
		// // 数据库新增操作
		// int insert = mapper.insert(yyyyMMddhhmmss);
		// if(b){
		// System.out.println(name+","+id+","+yyyyMMddhhmmss+"成功；；；；；；；；；；");
		// }else{
		// System.out.println(name+","+id+","+yyyyMMddhhmmss+"失败了————————————————————————");
		// }
		// } catch (Exception e) {
		// System.out.println(name+","+id+","+yyyyMMddhhmmss+"失败了————————————————————————");
		// throw new RuntimeException(e);
		// } finally {
		// // 判断锁是否存在
		// boolean locked = lock.isLocked();
		// // 判断锁是否被当前线程保持
		// boolean heldByCurrentThread = lock.isHeldByCurrentThread();
		// log.info("{}：获取锁状态：{} 是否当前线程保留：{}", name, locked, heldByCurrentThread);
		// if (locked && heldByCurrentThread) {
		// lock.unlock();
		// System.out.println(name+","+id+","+yyyyMMddhhmmss+"释放锁}}}}}}}}}}}}}}}}");
		// } else {
		// log.info("{}：未获得锁不用释放", name);
		// }
		//
		// }

	}

	@Override
	public List<LogsVO> selectList() {
		return mapper.list();
	}

}
